package com.rain.spit.controller;

import com.rain.spit.pojo.Spit;
import com.rain.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 吐槽的表现层
 * @auther 德哲
 * @date 2019/4/28 14:28.
 */
@RestController
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Spit> list = spitService.findAll();
        return new Result(true, StatusCode.OK,"执行成功",list);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method = RequestMethod.GET)
    public Result findOne(@PathVariable("id") String id){
        Spit spit = spitService.findById(id);
        return new Result(true, StatusCode.OK,"执行成功",spit);
    }

    /**
     * 保存
     * @param spit
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Spit spit){
        spitService.save(spit);
        return new Result(true, StatusCode.OK,"执行成功");
    }

    /**
     * 更新
     * @param id
     * @param spit
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable("id") String id,@RequestBody Spit spit){
        spit.set_id(id);
        spitService.update(spit);
        return new Result(true, StatusCode.OK,"执行成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") String id){
        spitService.delete(id);
        return new Result(true, StatusCode.OK,"执行成功");
    }

    /**
     * 根据父级ID查询吐槽列表
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/comment/{parentid}/{page}/{size}",method = RequestMethod.GET)
    public Result findByParentid(@PathVariable("parentid") String parentid,@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Spit> page1 = spitService.findByParentid(parentid,page,size);
        PageResult<Spit> spitPageResult = new PageResult<>(page1.getTotalElements(),page1.getContent());
        return new Result(true, StatusCode.OK,"执行成功",spitPageResult);
    }

    /**
     * 吐槽点赞
     */
    @RequestMapping(value = "/thumbup/{id}",method = RequestMethod.PUT)
    public Result updateThumbup(@PathVariable("id") String id){
        spitService.updateThumbup(id);
        return new Result(true, StatusCode.OK,"执行成功");
    }
}
